package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.MarketBrand;
import com.cskaoyan.bean.MarketBrandExample;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.service.admin.AdminBrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author :rain
 * @description
 * @since 2022/06/26 20:31
 */
@Service
public class AdminBrandServiceImpl implements AdminBrandService {

    @Autowired
    MarketBrandMapper marketBrandMapper;

    //全部显示和点击查找用的是同一个方法，所以要上传的参数众多了id，以及name
    @Override
    public CommonData<MarketBrand> queryAllBrandInfo(String id, String name, BasePageInfo info) {
        //开启分页插件
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            try {
                //拼接id
                criteria.andIdEqualTo(Integer.parseInt(id));
            } catch (Exception e) {
                return null;
            }

        }
        if (!StringUtils.isEmpty(name)) {
            name = "%" + name.trim() + "%";
            //拼接name
            criteria.andNameLike(name);
        }
        //默认显示的为deleted的值为false,便是未删除的信息
        criteria.andDeletedEqualTo(false);
        //通过具体的信息查询
        List<MarketBrand> marketBrandList = marketBrandMapper.selectByExample(marketBrandExample);

        //将所有的list信息进行分页
        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo(marketBrandList);
        return CommonData.data(marketBrandPageInfo);
    }

    //修改数据，并返回当前修改的对象
    @Override
    public MarketBrand modifyMarkBrand(MarketBrand marketBrand) {
        marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        MarketBrand marketBrand1 = marketBrandMapper.selectByPrimaryKey(marketBrand.getId());
        return marketBrand1;
    }

   //默认增加的数据deleted的值为false
    @Override
    public MarketBrand insertNewInfo(MarketBrand marketBrand) {
        //直接插入
        marketBrand.setDeleted(false);
        marketBrandMapper.insertSelective(marketBrand);
        //根据markBrand的id获取，增加了了useGenerate
        MarketBrand marketBrand1 = marketBrandMapper.selectByPrimaryKey(marketBrand.getId());
        return marketBrand1;
    }

    //并不是真正的删除，而是将deleted置为true,表示已经删除
    @Override
    public Integer delete(MarketBrand marketBrand) {
        marketBrand.setDeleted(true);
        int i = marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        if (i > 0){
            return 200;
        }
            return 500;
    }
}
